package com.xinjiang.electricpower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.StringUtils;
import com.xinjiang.electricpower.domain.DTO.SysOrgDTO;
import com.xinjiang.electricpower.domain.SysOrg;
import com.xinjiang.electricpower.domain.SysOrgInfo;
import com.xinjiang.electricpower.domain.vo.OrgConfigVO;
import com.xinjiang.electricpower.mapper.SysOrgMapper;
import com.xinjiang.electricpower.service.ISysOrgInfoService;
import com.xinjiang.electricpower.service.ISysOrgService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author feizhao
 * @date 2022/12/6
 * @apiNote
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> implements ISysOrgService {
    private final ISysOrgInfoService sysOrginfoService;
    @Override
    public IPage<OrgConfigVO> getSysOrgList(IPage<OrgConfigVO> page, SysOrgDTO sysOrgDTO) {
        QueryWrapper<OrgConfigVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(sysOrgDTO.getOrgNo()), "so.org_no", sysOrgDTO.getOrgNo());
        queryWrapper.like(StringUtils.hasText(sysOrgDTO.getOrgName()), "so.org_name", sysOrgDTO.getOrgName());
        queryWrapper.orderByDesc("longtitudeAndLatitude");
        return baseMapper.selectSysOrgInfoList(page, queryWrapper);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateOrgNoInfoById(SysOrgDTO sysOrgDTO) {
        SysOrgInfo sysOrgInfo = new SysOrgInfo();
        if(StringUtils.isNotEmpty(sysOrgDTO.getLongtitudeAndLatitude())){
        List<String> list = Arrays.asList(sysOrgDTO.getLongtitudeAndLatitude().split(","));

        sysOrgInfo.setId(sysOrgDTO.getOrgInfoId()).setOrgNo(sysOrgDTO.getOrgNo()).setLongtitude(list.get(0)).setLatitude(list.get(1));
        }else{
            sysOrgInfo.setId(sysOrgDTO.getOrgInfoId()).setOrgNo(sysOrgDTO.getOrgNo()).setLongtitude("").setLatitude("");
        }
        return sysOrginfoService.saveOrUpdate(sysOrgInfo);
    }
}
